home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8174 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  51 lines

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: microseconds??
  5. Date: 1 Mar 1996 12:42:05 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h7netINN1hk@anvil.ugrad.cs.ubc.ca>
  8. References: <4h62s9$2o7@chaos.dac.neu.edu>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4h62s9$2o7@chaos.dac.neu.edu>,
  12. hadi abedi <habedi@lynx.dac.neu.edu> wrote:
  13. >Hello,
  14. >    Thanks for taking your time to read this.
  15. >    
  16. >    Is there a way to compute microsecond intervals    
  17. >    with C?
  18. >    I'd like to time a sorting routine on different
  19. >    list sizes. I am aware of the 'time' function on
  20. >    the UNIX system, but I'd like to know how to do 
  21. >    it in C.
  22. >    Thanks.
  23.  
  24. The _best_ way to do this under a particular environment is up to, well, that
  25. particular environment. The standard C way of timing is the the clock()
  26. function. Converting to seconds means dividing by the symbolic constant
  27. CLOCKS_PER_SEC defined in <time.h>. Hopefully, this is a few order of
  28. magnitudes greater than 1.
  29.  
  30. On this HP-UX system, the actual value is:
  31.  
  32. /* ANSI C time constants, types, and structures */
  33.  
  34. #ifdef _INCLUDE__STDC__
  35. #  define CLOCKS_PER_SEC 1000000
  36.  
  37. #  ifndef NULL
  38. #    define NULL 0
  39. #  endif
  40.  
  41.  
  42. So you see, the clock() function can have microsecond resolution (I'm not
  43. saying that the hardware does, just the clock() function).
  44.  
  45. It's likely that it is implemented with the best possible method that yields
  46. the greatest resolution. The above clearly shows that CLOCKS_PER_SEC does not
  47. have to be restricted to the preemptive clock frequency of the operating system
  48. (i.e. the HZ value).
  49. -- 
  50.  
  51.